package twitter.clienttest;

import java.util.concurrent.locks.ReentrantReadWriteLock;

import org.apache.log4j.*;

import twitter.TwitterLibrary.*;
public class ClientTest {
	private String server="";
	private int port=10001;
	private final ReentrantReadWriteLock rwl=new ReentrantReadWriteLock();
	static Logger logger = Logger.getLogger(ClientTest.class);
	private Utility utility;
	public ClientTest()
	{
		utility=new Utility(rwl);
		utility.setLogger(logger, "client.log");
		
		
	}
	public static void main(String[] args)
	{

		
		ClientTest test=new ClientTest();

		if(args.length<2)
		{
			System.out.println("please input the server address and the server port");
		}
		else
		{
			test.server=args[0];
			test.port=Integer.parseInt(args[1]);
			for(int i=0;i<5;i++)
			{
				test.concurrentRequest();
			}
		}

	}
	public void concurrentRequest()
	{
		Thread t1=new Thread(new Runnable(){
			
			public void run()
			{
				logger.info("test client thread t1 started run");
				testClient("#san #fancisco is a nice #city","city","thread t0");
			}

			
		});
		Thread t2=new Thread(new Runnable(){
			public void run()
			{
				logger.info("test client thread t2 started run");
				testClient("today is a #rainy day","rainy","thread t1");
			}
		});
		Thread t3=new Thread(new Runnable(){
			public void run()
			{
				logger.info("test client thread t3 started run");
				testClient("I am hunting #internship now","internship","thread t2");
			}
		});
		t1.start();
		t2.start();
		t3.start();
	}
	
	public void testClient(String status,String tag,String threadname)
	{
	TwitterClient client=new TwitterClient(server,port,threadname);
		
		client.sendHttpPostRequest(status);
		bigSpace();
		
		client.sendHttpGetRequest(tag);
		bigSpace();
		
		client.sendHttpGetRequest(tag);
		bigSpace();
		
		client.sendHttpDeleteRequest();
		bigSpace();
		
		client.sendHttpPutRequest();
		bigSpace();
		
		client.sendIncorrectFormatGet("incorrect format");
		bigSpace();
		
		client.sendIncorrectFormatPost("this is a #incorrect format post");
		bigSpace();
		
		client.sendIncorrectURIGet("uri");
		bigSpace();
		
		client.sendIncorrectURIPost("this is a #incorrent #uri post");
		bigSpace();
		
		client.sendHttpGetIncorectVersion("version");
		
	}
	
	
	private void bigSpace()
	{
		System.out.println("");
		System.out.println("");
		System.out.println("");
	}
	
}
